package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catalog2VO;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {

        //TODO 查出所有分类是一级分类
        List<CategoryEntity> categoryEntityList = categoryService.getLeval1Category();
        //视图解析器进行拼串
        //classpath：/template/+返回值+.html
        model.addAttribute("categories", categoryEntityList);
        return "index";

    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2VO>> getCatalogJSON() {
        Map<String, List<Catalog2VO>> map = categoryService.getCatalogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        //1.获得一把，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("my-lock");

        //加锁
        lock.lock();
        try {
            System.out.println("加锁");
            Thread.sleep(30000);
        } catch (Exception e) {

        } finally {
            //解锁，将解锁没有运行的，
            System.out.println("释放");
            lock.unlock();
        }

        return "hello";

    }

    @GetMapping("/write")
    @ResponseBody
    public String writeValue() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock writeLock = lock.writeLock();

        try {
            writeLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(1000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
        return s;
    }


    @GetMapping("/read")
    @ResponseBody
    public String readValue() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.readLock();
        try {

            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.acquire();//获取一个信号获取一个值，占一个车位

        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();//释放一个车位

        return "ok";
    }


    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");

        door.trySetCount(5);
        door.await();//等待闭锁都完成
        return "啊，放假了~~~";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String go(@PathVariable("id") Long id) {

        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();//计数减一

        return id + "班放假了";

    }

}
